#import "XMAddressViewCell.h"
#import <Masonry/Masonry.h>

@interface XMAddressViewCell ()
@property (nonatomic, strong) UILabel *namePhoneLabel;     // 新增：姓名和电话标签
@property (nonatomic, strong) UILabel *defaultLabel;       // 默认标签
@property (nonatomic, strong) UILabel *addressLabel;       // 地址标签
@property (nonatomic, strong) UIButton *defaultButton;     // 默认按钮

@property (nonatomic, strong) UIButton *setDefalutButton;     // 设为默认按钮

@end

@implementation XMAddressViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    
    // 创建内容视图
    UIView *contentBgView = [[UIView alloc] init];
    contentBgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:contentBgView];
    
    // 姓名和电话标签
    _namePhoneLabel = [[UILabel alloc] init];
    _namePhoneLabel.font = [UIFont systemFontOfSize:16];
    [contentBgView addSubview:_namePhoneLabel];
    
    // 默认标签
    _defaultLabel = [[UILabel alloc] init];
    _defaultLabel.text = @"默认";
    _defaultLabel.font = [UIFont systemFontOfSize:12];
    _defaultLabel.textColor = [UIColor colorWithRed:0.92 green:0.23 blue:0.19 alpha:1.0];
    _defaultLabel.backgroundColor = [UIColor colorWithRed:0.99 green:0.87 blue:0.86 alpha:1.0];
    _defaultLabel.layer.cornerRadius = 3;
    _defaultLabel.layer.masksToBounds = YES;
    _defaultLabel.textAlignment = NSTextAlignmentCenter;
    [contentBgView addSubview:_defaultLabel];
    
    // 地址标签
    _addressLabel = [[UILabel alloc] init];
    _addressLabel.font = [UIFont systemFontOfSize:14];
    _addressLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    _addressLabel.numberOfLines = 0;
    [contentBgView addSubview:_addressLabel];
    
    // 默认按钮
    _defaultButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_defaultButton setImage:[UIImage imageNamed:@"radio_unselected"] forState:UIControlStateNormal];
    [_defaultButton setImage:[UIImage imageNamed:@"radio_selected"] forState:UIControlStateSelected];
    [_defaultButton addTarget:self action:@selector(defaultButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [contentBgView addSubview:_defaultButton];
    
    
    // 编辑按钮
    UIButton *setDefalutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setDefalutBtn setTitle:@"设为默认" forState:UIControlStateNormal];
    [setDefalutBtn setTitleColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0] forState:UIControlStateNormal];
    [setDefalutBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [setDefalutBtn addTarget:self action:@selector(editButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [contentBgView addSubview:setDefalutBtn];
    _setDefalutButton = setDefalutBtn;
    
    
    // 编辑按钮
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editButton setTitle:@"编辑地址" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0] forState:UIControlStateNormal];
    [editButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [editButton addTarget:self action:@selector(editButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [contentBgView addSubview:editButton];
    
    // 删除按钮
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [deleteButton setTitleColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0] forState:UIControlStateNormal];
    [deleteButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [deleteButton addTarget:self action:@selector(deleteButtonTapped) forControlEvents:UIControlEventTouchUpInside];
//    [contentBgView addSubview:deleteButton];
    
    // 设置约束
    [contentBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(4);
        make.left.equalTo(self.contentView).offset(0);
        make.right.equalTo(self.contentView).offset(0);
        make.bottom.equalTo(self.contentView).offset(-4);
    }];
    
    [_namePhoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentBgView).offset(15);
        make.left.equalTo(contentBgView).offset(17);
    }];
    
    [_defaultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentBgView).offset(15);
        make.top.equalTo(_namePhoneLabel.mas_bottom).offset(8);

//        make.left.equalTo(_namePhoneLabel.mas_right).offset(8);
//        make.centerY.equalTo(_namePhoneLabel);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(18);
    }];
    
    [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_namePhoneLabel.mas_bottom).offset(8);
        make.left.equalTo(_defaultLabel.mas_right).offset(9);
        make.right.equalTo(contentBgView).offset(-15);
    }];
    
    [_defaultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentBgView).offset(15);
        make.bottom.equalTo(contentBgView).offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [_setDefalutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_addressLabel).offset(0);
        make.bottom.equalTo(contentBgView).offset(-15);
//        make.top.equalTo(_addressLabel.mas_bottom).offset(16);
        make.height.mas_equalTo(20);
    }];
    
    [editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(contentBgView).offset(-45);
        make.bottom.equalTo(contentBgView).offset(-15);
        make.height.mas_equalTo(20);
    }];
    
//    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(contentBgView).offset(-15);
//        make.bottom.equalTo(contentBgView).offset(-15);
//        make.size.mas_equalTo(CGSizeMake(40, 20));
//    }];
}

- (void)configureWithAddress:(NSDictionary *)address {
    NSString *name = address[@"name"];
    NSString *phone = address[@"phone"];
    NSString *addressDetail = address[@"address"];
    BOOL isDefault = [address[@"isDefault"] boolValue];
    
    // 格式化姓名和电话
    _namePhoneLabel.text = [NSString stringWithFormat:@"%@      %@", name, [self formatPhoneNumber:phone]];
    
    // 设置地址
    _addressLabel.text = addressDetail;
    
    // 设置默认状态
    _defaultButton.selected = isDefault;
//    _defaultLabel.hidden = !isDefault;
}

- (NSString *)formatPhoneNumber:(NSString *)phone {
    if (phone.length != 11) {
        return phone;
    }
    
    return [NSString stringWithFormat:@"%@ **** %@",
            [phone substringToIndex:3],
            [phone substringFromIndex:7]];
}

#pragma mark - Button Actions

- (void)defaultButtonTapped {
    if ([self.delegate respondsToSelector:@selector(addressCell:didSelectDefault:)]) {
        [self.delegate addressCell:self didSelectDefault:self.indexPath];
    }
}

- (void)editButtonTapped {
    if ([self.delegate respondsToSelector:@selector(addressCell:didClickEdit:)]) {
        [self.delegate addressCell:self didClickEdit:self.indexPath];
    }
}

- (void)deleteButtonTapped {
    if ([self.delegate respondsToSelector:@selector(addressCell:didClickDelete:)]) {
        [self.delegate addressCell:self didClickDelete:self.indexPath];
    }
}

@end    
